/*
 * @Author: huangpx
 * @Date: 2021-09-22 15:38:08
 * @LastEditors: huangpx
 * @LastEditTime: 2023-04-22 18:00:34
 * @Description: axios请求
 */
import axios from './axios';
// 创建axios实例
const request = axios.create(
  {
    headers: {
      'Content-Type': 'application/json'
    }
  }
);
request.interceptors.request.use(
  (config) => {
    // test();
    let token = sessionStorage.getItem('token');
    if (token) {
      config.headers.Authorization = 'Bearer ' + token;
    }
    return config;
  },
  (error) => {
    // 404等问题可以在这里处理
    // eslint-disable-next-line no-undef
    return Promise.reject(error);
  }
);
request.interceptors.response.use(
  function (response) {
    return response;
  },
  function(error) {
    // 请求错误则向store commit这个状态变化
    // const httpError = {
    //   hasError: true,
    //   status: error.response.status,
    //   statusText: error.response.statusText,
    // };
    if (error.response.status === 401) {
      location.reload();
      return false;
    }
    console.log('接口出错', error);
    // eslint-disable-next-line no-undef
    return Promise.reject(error);
  }
);
export default request;
